package com.huadao.cart_ccs.netty;

import android.util.Log;

import com.huadao.cart_ccs.common.ByteUtils;
import com.huadao.cart_ccs.data.bo.Protocol;
import com.huadao.cart_ccs.exception.BytesLengthException;
import com.huadao.cart_ccs.exception.ProtocolException;
import com.huadao.cart_ccs.exception.StructureException;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class MyDecoder extends ByteToMessageDecoder {

    private static final String TAG="MyDecoder";

    private static int headLength=1;
    private static int lengthLength=2;
    private static int agentNumberLength=2;
    private static int MACLength=6;
    private static int deviceTypeLength=1;
    private static int commandWordLength=1;
    private static int modularOrderLength=3;
    private static int orderContentLength;
    private static int checkSumLength=1;
    private static int tailLength=1;

    private static final byte[] HEAD=new byte[]{(byte) 0xBA};
    private static final byte[] TAIL=new byte[]{(byte) 0xED};

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws BytesLengthException, StructureException  {

        String responseStr=ByteUtils.getHexByte(in.array(),0,in.readableBytes());
        Log.i(TAG,responseStr);

        int length=in.readableBytes();
        if(length<headLength+tailLength){
            in.clear();
            throw new BytesLengthException("response length:"+ length + "too short");
        }

        byte[] head= in.readBytes(1).array();//报头
        if(!ByteUtils.isEqual(head,HEAD)){
            in.clear();
            throw new StructureException("wrong head:"+ ByteUtils.getHexByte(head,0,head.length));
        }
        byte[] resLength;
        if(in.readableBytes()<lengthLength){
            in.clear();
            throw new StructureException("wrong content length");
        }else {
            resLength = in.readBytes(lengthLength).array();//报文长度
            int contentLength=ByteUtils.bytes2intHL(resLength);
            if(contentLength+headLength+tailLength+lengthLength!=length){
                in.clear();
                throw new StructureException("wrong length");
            }
            orderContentLength=contentLength-agentNumberLength-MACLength-deviceTypeLength-commandWordLength-modularOrderLength-checkSumLength;
        }
        byte[] agentNumber;
        if(in.readableBytes()<agentNumberLength){
            in.clear();
            throw new StructureException("wrong agentNumber");
        }else {
            agentNumber=in.readBytes(agentNumberLength).array();//代理商编号
        }
        byte[] mac;
        if(in.readableBytes()<MACLength){
            in.clear();
            throw new StructureException("wrong MAC");
        }else {
            mac=in.readBytes(MACLength).array();//MAC地址
        }
        byte[] deviceType;
        if(in.readableBytes()<deviceTypeLength){
            in.clear();
            throw new StructureException("wrong deviceType");
        }else {
            deviceType=in.readBytes(deviceTypeLength).array();//设备类型
        }
        byte[] commandWord;
        if(in.readableBytes()<commandWordLength){
            in.clear();
            throw new StructureException("wrong commandWord");
        }else {
            commandWord=in.readBytes(commandWordLength).array();//命令字
        }
        byte[] modularOrder;
        if(in.readableBytes()<modularOrderLength){
            in.clear();
            throw new StructureException("wrong modularOrder");
        }else {
            modularOrder=in.readBytes(modularOrderLength).array();//模块指令
        }
        byte[] orderContent;
        if(in.readableBytes()<orderContentLength){
            in.clear();
            throw new StructureException("wrong orderContent");
        }else {
            orderContent=in.readBytes(orderContentLength).array();//指令内容
        }
        byte[] checkSum;
        if(in.readableBytes()<checkSumLength){
            in.clear();
            throw new StructureException("wrong checkSum");
        }else {
            checkSum=in.readBytes(checkSumLength).array();//校验和
        }
        byte[] tail;
        if(in.readableBytes()<tailLength){
            in.clear();
            throw new StructureException("wrong tail");
        }else {
            tail=in.readBytes(tailLength).array();//报尾
            if(!ByteUtils.isEqual(tail,TAIL)){
                in.clear();
                throw new StructureException("wrong tail:"+ ByteUtils.getHexByte(head,0,tail.length));
            }
        }

        Protocol protocol=new Protocol(head,resLength,agentNumber,mac,deviceType,commandWord,modularOrder,orderContent,checkSum,tail);
        try {
            out.add(protocol.parse2BO());
        } catch (ProtocolException e) {
            e.printStackTrace();
        }

//        int index=0;
//        byte[] response=in.array();
//        byte[] head= Arrays.copyOfRange(response,index,index+=headLength);//报头
//        if(!ByteUtils.isEqual(head,HEAD)){
//            throw new StructureException("wrong head:"+ ByteUtils.getHexByte(head,0,head.length));
//        }
//
//        byte[] resLength=Arrays.copyOfRange(response,index,index+=lengthLength);//报文长度
//        int contentLength=ByteUtils.bytes2intHL(resLength);
//        orderContentLength=contentLength-agentNumberLength-MACLength-deviceTypeLength-commandWordLength-modularOrderLength-checkSumLength;
//
//        byte[] agentNumber=Arrays.copyOfRange(response,index,index+=agentNumberLength);//代理商编号
//        byte[] mac=Arrays.copyOfRange(response,index,index+=MACLength);//MAC地址
//        byte[] deviceType=Arrays.copyOfRange(response,index,index+=deviceTypeLength);//设备类型
//        byte[] commandWord=Arrays.copyOfRange(response,index,index+=commandWordLength);//命令字
//        byte[] modularOrder=Arrays.copyOfRange(response,index,index+=modularOrderLength);//模块指令
//        byte[] orderContent=Arrays.copyOfRange(response,index,index+=orderContentLength);//指令内容
//        byte[] checkSum=Arrays.copyOfRange(response,index,index+=checkSumLength);//校验和
//        byte[] tail=Arrays.copyOfRange(response,index,index+=tailLength);//报尾
//        if(!ByteUtils.isEqual(tail,TAIL)){
//            throw new StructureException("wrong tail:"+ ByteUtils.getHexByte(tail,0,tail.length));
//        }
//        String responseStr=ByteUtils.getHexByte(response,0,index);
//        Log.i(TAG,responseStr);
//        Protocol protocol=new Protocol(head,resLength,agentNumber,mac,deviceType,commandWord,modularOrder,orderContent,checkSum,tail);
//
//        out.add(protocol.parse2BO());
    }


}